package org.chatboleh.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import org.apache.log4j.Logger;
import org.chatboleh.ApplicationFrame;
import org.chatboleh.database.ConnectionFactory;
import org.chatboleh.database.ServerManager;
import org.chatboleh.exception.EDMException;
import org.chatboleh.util.MessageBox;

public class ApplicationFrameController {
	private Logger logger = Logger.getLogger(this.getClass());
	private ApplicationFrame view;
	
	public ApplicationFrameController(ApplicationFrame component){
		this.view = component;
	}
	
	public class MenuExitMouseListener extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent event) {
			view.setVisible(false);
//			logger.debug("mousePressed");
			// MessageBox.info("Bye bye!");
			try {
				ConnectionFactory.CloseConnection();
			} catch (EDMException e) {
				logger.error("",e);
			}
			ServerManager.stopServer();
			view.dispose();
		}

	}
	
	public class WindowEventAdapter extends WindowAdapter {

		@Override
		public void windowClosing(WindowEvent event) {
			view.setVisible(false);
			view.setEnabled(false);
			try {
				ConnectionFactory.CloseConnection();
			} catch (EDMException e) {
				e.printStackTrace();
			}
			ServerManager.stopServer();
		}

		@Override
		public void windowOpened(WindowEvent event) {
			ServerManager.startServer();
			try {
				ConnectionFactory.getInstance();
			} catch (EDMException e) {
				e.printStackTrace();
			}
		}
	}
	
	public class LoginFrameHiddenAdapter extends ComponentAdapter {
		@Override
		public void componentHidden(ComponentEvent e) {
//				MessageBox.info("Login Success", "Something");
				view.setTitle("Login as: " + view.getUser().getUsername());
				view.ShowUserMenu();
				view.ShowPayeeList();
				view.ShowDocumentList();
		}
	}
	
	public class PayeeListActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent ae) {
			if (ae.getActionCommand().equals("Payee List")) {
				view.ShowPayeeList();
			}
		}
	}
	
	public class DocumentListActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent ae) {
			if (ae.getActionCommand().equals("Document List")) {
				view.ShowDocumentList();
			}
		}
	}
	
	public class LogoutActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// Logout
			view.setUser( null );
			view.setTitle("Please Login");
			// Close all InnerFrame
			view.getJDesktopPane0().removeAll();
			view.getJDesktopPane0().repaint();
			// Remove user menu
			view.getJMenuBar1().remove(1);
			view.getJMenu0().remove(0);
			// Show login form
			view.ShowLoginFrame();
		}
		
	}
}
